package com.qf.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    // 处理业务的交换机
    @Bean
    public TopicExchange userExchange(){
        return new TopicExchange("user-exchange");
    }

    // 创建业务队列
    @Bean
    public Queue userQueue(){

        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","dlq-exchange"); // 设置死信交换机
        map.put("x-dead-letter-routing-key","dlq.error"); // 设置死信交换机
        return new Queue("user-queue",true,false,false,map);
    }

    // 把队列绑定到交换机
    @Bean
    public Binding userQueueToUserExchange(Queue userQueue,TopicExchange userExchange){
        return BindingBuilder.bind(userQueue).to(userExchange).with("user.*");
    }

    // 死信交换机
    @Bean
    public TopicExchange dlqExchange(){
        return new TopicExchange("dlq-exchange");
    }

    // 死信队列
    @Bean
    public Queue dlqQueue(){
        return new Queue("dlq-queue",true,false,false,null);
    }

    // 绑定
    @Bean
    public Binding dlqQueueToDlqExchange(Queue dlqQueue,TopicExchange dlqExchange){
        return BindingBuilder.bind(dlqQueue).to(dlqExchange).with("dlq.*");
    }

}
